package ui.prototypes;

import android.app.Fragment;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CashSummaryFragment extends Fragment {

    private String mNumberFormat;

    private TextView mTxtIncoming;

    private TextView mTxtOutcoming;

    private TextView mTxtTotal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       mNumberFormat = getActivity().getString(R.string.real_number_format);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.cash_summary, container, false);

        mTxtIncoming  = (TextView) view.findViewById(R.id.incoming_field);
        mTxtOutcoming = (TextView) view.findViewById(R.id.outcoming_field);
        mTxtTotal     = (TextView) view.findViewById(R.id.total_field);

        return view;
    }

    protected class CashSummaryAdapter extends ContentObserver {

        private final Cursor mCursor;

        public CashSummaryAdapter(Cursor c) {
            super(new Handler());
            mCursor = c;
        }

        public void bindView() {
            double incoming = mCursor.getDouble(mCursor.getColumnIndex(CashFlowContract.CashFlowTable.INCOMING));
            double outcoming = mCursor.getDouble(mCursor.getColumnIndex(CashFlowContract.CashFlowTable.OUTCOMING));
            double total = incoming - outcoming;

            mTxtIncoming.setText(String.format(mNumberFormat, incoming));
            mTxtOutcoming.setText(String.format(mNumberFormat, outcoming));
            mTxtTotal.setText(String.format(mNumberFormat, total));
        }

        @Override
        public void onChange(boolean selfChange) {
            bindView();
        }

    }

}
